/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cjava.system.model;

import java.io.File;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.omnifaces.util.Faces;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author user
 */
@Entity
@Table(name = "person", catalog = "socioscjava", schema = "")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Person.findAll", query = "SELECT p FROM Person p")})
public class Person implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 8)
    @Column(name = "dni")
    private String dni;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 25)
    @Column(name = "name")
    private String name;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 25)
    @Column(name = "father_name")
    private String fatherName;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 25)
    @Column(name = "mother_name")
    private String motherName;
    // @Pattern(regexp="^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$", message="Invalid phone/fax format, should be as xxx-xxx-xxxx")//if the field contains phone or fax number consider using this annotation to enforce field validation
    @Size(max = 20)
    @Column(name = "phone")
    private String phone;
    @Size(max = 20)
    @Column(name = "phone2")
    private String phone2;
    // @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 50)
    @Column(name = "email")
    private String email;
    @Column(name = "start_date")
    @Temporal(TemporalType.DATE)
    private Date startDate;
    @Size(max = 100)
    @Column(name = "address_home")
    private String addressHome;
    @Size(max = 100)
    @Column(name = "working_data")
    private String workingData;
    @Column(name = "ocupation")
    private String ocupation;
    @Column(name = "curriculum_url")
    private String curriculumUrl;
    @Column(name = "photo")
    private String photo;
    @Column(name = "photo_check")
    private Boolean photoCheck = false;
    @Column(name = "birthday")
    @Temporal(TemporalType.DATE)
    private Date birthday;
    @JoinColumn(name = "id_university", referencedColumnName = "id")
    @ManyToOne
    private University idUniversity;
    @JoinColumn(name = "id_district", referencedColumnName = "id")
    @ManyToOne
    private District idDistrict;
    @OneToMany(mappedBy = "idPerson")
    @Cascade({CascadeType.DELETE_ORPHAN, CascadeType.SAVE_UPDATE, CascadeType.DELETE})
    private List<BusinessAssociate> businessAssociateList;
    @OneToMany(mappedBy = "idPerson")
    @Cascade({CascadeType.DELETE_ORPHAN, CascadeType.SAVE_UPDATE, CascadeType.DELETE})
    private List<MeetingPerson> meetingPersonList;
    @OneToMany(mappedBy = "idPerson")
    @Cascade({CascadeType.DELETE_ORPHAN, CascadeType.SAVE_UPDATE, CascadeType.DELETE})
    private List<PersonProfile> personProfileList = new ArrayList<>();

    @Transient
    private String completeName;
    @Transient
    private StreamedContent fileDownload; 

    public Person() {
    }

    public Person(Integer id) {
        this.id = id;
    }

    public Person(Integer id, String dni, String name, String fatherName, String motherName, String email) {
        this.id = id;
        this.dni = dni;
        this.name = name;
        this.fatherName = fatherName;
        this.motherName = motherName;
        this.email = email;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getDni() {
        return dni;
    }

    public void setDni(String dni) {
        this.dni = dni;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getFatherName() {
        return fatherName;
    }
    
    @Transient
    public boolean getHaveCV() {
        return this.curriculumUrl != null && !this.curriculumUrl.isEmpty();
    }

    public void setFatherName(String fatherName) {
        this.fatherName = fatherName;
    }

    public String getCurriculumUrl() {
        return curriculumUrl;
    }

    public void setCurriculumUrl(String curriculumUrl) {
        this.curriculumUrl = curriculumUrl;
    }

    public String getMotherName() {
        return motherName;
    }

    public void setMotherName(String motherName) {
        this.motherName = motherName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPhone2() {
        return phone2;
    }

    public void setPhone2(String phone2) {
        this.phone2 = phone2;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public String getAddressHome() {
        return addressHome;
    }

    public void setAddressHome(String addressHome) {
        this.addressHome = addressHome;
    }

    public String getWorkingData() {
        return workingData;
    }

    public void setWorkingData(String workingData) {
        this.workingData = workingData;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public University getIdUniversity() {
        return idUniversity;
    }

    public Boolean getPhotoCheck() {
        return photoCheck;
    }

    public Boolean isPhotoCheck() {
        return photoCheck;
    }
    
    public void setPhotoCheck(Boolean photoCheck) {
        this.photoCheck = photoCheck;
    }

    public void setIdUniversity(University idUniversity) {
        this.idUniversity = idUniversity;
    }

    public District getIdDistrict() {
        return idDistrict;
    }

    public void setIdDistrict(District idDistrict) {
        this.idDistrict = idDistrict;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public String getOcupation() {
        return ocupation;
    }

    public void setOcupation(String ocupation) {
        this.ocupation = ocupation;
    }

    public StreamedContent getFileDownload() {
        String fullPath = "/resources/files/cv/" + this.getCurriculumUrl();
        fullPath = fullPath.replace("/", File.separatorChar + "");
        InputStream stream = Faces.getResourceAsStream(fullPath);  
        fileDownload = new DefaultStreamedContent(stream, "application/pdf", "cv-" + this.getCompleteName() +".pdf");
        return fileDownload;
    }

    public void setFileDownload(StreamedContent fileDownload) {
        this.fileDownload = fileDownload;
    }

    public String getCompleteName() {
        completeName = fatherName + " " + motherName + ", " + name;
        return completeName;
    }

    public void setCompleteName(String completeName) {
        this.completeName = completeName;
    }

    @XmlTransient
    public List<BusinessAssociate> getBusinessAssociateList() {
        return businessAssociateList;
    }

    public void setBusinessAssociateList(List<BusinessAssociate> businessAssociateList) {
        this.businessAssociateList = businessAssociateList;
    }

    @XmlTransient
    public List<MeetingPerson> getMeetingPersonList() {
        return meetingPersonList;
    }

    public void setMeetingPersonList(List<MeetingPerson> meetingPersonList) {
        this.meetingPersonList = meetingPersonList;
    }

    @XmlTransient
    public List<PersonProfile> getPersonProfileList() {
        return personProfileList;
    }

    public void setPersonProfileList(List<PersonProfile> personProfileList) {
        this.personProfileList = personProfileList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Person)) {
            return false;
        }
        Person other = (Person) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "org.cjava.system.model.Person[ id=" + id + " ]";
    }

}
